package com.practice.car.cardataapp.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.practice.car.cardataapp.entity.CarSaleMonthTop10;
import com.practice.car.cardataapp.entity.NationalCarSalesTrends;
import com.practice.car.cardataapp.entity.frontEnd.Form;
import com.practice.car.cardataapp.mapper.CarSaleMonthTop10Dao;
import com.practice.car.cardataapp.service.CarSaleMonthTop10Service;
import lombok.Data;
import org.springframework.stereotype.Service;
import scala.Tuple2;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * car-data-app project
 *
 * @author ZhangMingXing
 * @date 2020/9/10
 */
@Service
public class CarSaleMonthTop10ServiceImpl extends ServiceImpl<CarSaleMonthTop10Dao,CarSaleMonthTop10> implements CarSaleMonthTop10Service {
	@Override
	public List<Form> getSeries( ) {
		List<CarSaleMonthTop10> list = this.list();
		list.sort((a,b)->{
			String[] aStrings = a.getMonth().split("-");
			String[] bStrings = b.getMonth().split("-");
			Integer aYear = Integer.valueOf(aStrings[0]);
			Integer bYear = Integer.valueOf(bStrings[0]);
			if (!Objects.equals(aYear, bYear)) {
				return aYear - bYear;
			} else {
				Integer aMonth = Integer.valueOf(aStrings[1]);
				Integer bMonth = Integer.valueOf(bStrings[1]);
				return aMonth - bMonth;
			}
		});
		SeriesUtil<CarSaleMonthTop10> util = new SeriesUtil<>();
		util.setLegendSolver(CarSaleMonthTop10::getMonth);
		util.setFormSolver(e->new Tuple3(e.getName(),e.getBrand(),e.getSale()));
		return util.getSeries(list);
	}
	@Data
	class Tuple3{
		private String name="";
		private String brand="";
		private int sale = 0;

		public Tuple3(String name, String brand, int sale) {
			this.name = name;
			this.brand = brand;
			this.sale = sale;
		}
	}
}
